package com.chujian.framework.component;

import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.annotation.KeySequence;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.chujian.framework.component.def.YCol;
import com.chujian.framework.component.enums.DataType;
import io.swagger.annotations.ApiModelProperty;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

public class EntityHelper {

    public static EntityInfo collectEntityInfo(Class<?> entityClass) {
        String className = entityClass.getName();
        TableName tableName = entityClass.getAnnotation(TableName.class);
        KeySequence keySequence = entityClass.getAnnotation(KeySequence.class);
        return new EntityInfo()
                .setEntityClass(className)
                .setTableName(null != tableName ? tableName.value() : "")
                .setSeqName(null != keySequence ? keySequence.value() : "")
                .setCols(collectEntityCols(entityClass));
    }

    public static List<YCol> collectEntityCols(Class<?> entityClass) {
        List<YCol> cols = new ArrayList<>();
        for (Field field : ReflectUtil.getFields(entityClass)) {
            int modifiers = field.getModifiers();
            if (Modifier.isFinal(modifiers) || Modifier.isStatic(modifiers)) {
                continue;
            }
            TableId tableId = field.getAnnotation(TableId.class);
            TableField tableField = field.getAnnotation(TableField.class);
            if (null == tableId && null == tableField) {
                continue;
            }
            if (null != tableField && !tableField.exist()) {
                continue;
            }
            YCol col = new YCol();
            if (null != tableId) {
                col.setCol(tableId.value());
            }
            if (null != tableField) {
                col.setCol(tableField.value());
            }
            // 设置name
            ApiModelProperty modelProperty = field.getAnnotation(ApiModelProperty.class);
            if (null != modelProperty) {
                col.setName(modelProperty.value());
            }
            if (StrUtil.isEmpty(col.getName())) {
                col.setName(col.getCol());
            }
            col.setJavaField(field.getName())
                    .setJavaClass(field.getType().getName())
                    .setPk(null != tableId);
            col.setDataType(transDataType(field.getType()));
            cols.add(col);
        }
        return cols;
    }

    public static String transDataType(Class<?> fieldClass) {
        if (Number.class.isAssignableFrom(fieldClass)) {
            return DataType.Number.getName();
        }
        if (LocalDate.class.isAssignableFrom(fieldClass)) {
            return DataType.Date.getName();
        }
        if (LocalDateTime.class.isAssignableFrom(fieldClass)) {
            return DataType.DateTime.getName();
        }
        if (LocalTime.class.isAssignableFrom(fieldClass)) {
            return DataType.Time.getName();
        }
        return DataType.String.getName();
    }

}
